<template>
  <div class="tools">
    <div class="list">
      <a-carousel ref="carousel" :dots="false" :infinite="false" arrows>
        <div slot="prevArrow" class="custom-slick-arrow" style="left: 0;zIndex: 1">
          <img src="@/assets/images/arrow-1.png" alt="" />
        </div>
        <div slot="nextArrow" class="custom-slick-arrow" style="right: 24px;zIndex: 1">
          <img src="@/assets/images/arrow-2.png" alt="" />
        </div>

        <div v-for="(item, idx) in list" :key="idx" class="carousel-block">
          <a class="item" :target="value.fileUrl ? '_blank' : ''" :href="value.fileUrl ? value.fileUrl : 'javascript:;'" v-for="(value, key) in item" :key="key" @click="!value.fileUrl && urlWarning">
            <a-tooltip placement="bottom" :align="{ offset: [0, -30] }" arrow-point-at-center>
              <template slot="title">
                {{ value.remark }}
              </template>
              <div>
                <div class="item-icon">
                  <div class="img">
                    <img :src="value.filePath ? value.filePath : require('./images/tool-default.png')" alt="" />
                  </div>
                </div>
                <div class="label">{{ value.fileName }}</div>
              </div>
            </a-tooltip>
          </a>

          <template v-if="item.length < carouselStep">
            <div class="item" v-for="value in carouselStep - item.length" :key="'key' + value"></div>
          </template>
        </div>
      </a-carousel>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { Carousel, Tooltip } from "ant-design-vue";

@Component({
  name: "Tools",
  components: {
    ACarousel: Carousel,
    ATooltip: Tooltip
  }
})
export default class Tools extends Vue {
  private pageNum = 1;
  private total = 0;
  private pageSize = 200;
  private dataSource: any[] = [];
  // 一页显示几个项目
  private carouselStep = 8;

  private created() {
    this.getList();
  }

  private async getList() {
    const res = await this.$api.DownloadList({
      pageNum: this.pageNum,
      pageSize: this.pageSize,
      status: "0"
    });
    if (res && res.code === 200) {
      this.total = res.total;
      this.dataSource = res.rows;
    }
    return res;
  }

  private urlWarning() {
    this.$modal.warning({ content: "下载地址不存在！" });
  }

  private get list() {
    const step = this.carouselStep;
    const _return: any[] = Array.from({ length: Math.ceil(this.dataSource.length / step) }, () => []);
    for (let i = 0; i < _return.length; i++) {
      for (let j = i * step; j < i * step + step; j++) {
        if (this.dataSource[j]) {
          _return[i].push(this.dataSource[j]);
        } else {
          break;
        }
      }
    }
    return _return;
  }
}
</script>

<style lang="less" scoped>
@import "~@/assets/style/mixin.less";
.tools {
  .scale(height, 317px);
  overflow: hidden;
  .list {
    flex: 1;
    overflow: hidden;
  }

  .item-icon {
    .scale(width, 80px);
    .scale(height, 80px);
    .scale(border-radius, 80px);
    background: rgba(188, 202, 212, 0.1);
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    .scale(margin-bottom, 10px);
    .img {
      overflow: hidden;
      .scale(width, 50px);
      .scale(height, 50px);
      // .scale(border-radius, 50px);
      img {
        max-width: 100%;
        max-height: 100%;
      }
    }
  }

  .carousel-block {
    width: 100%;
    flex-shrink: 0;
    display: flex !important;
    flex-wrap: wrap;
    justify-content: space-between;
    transition-duration: 0.5s;
    .scale(padding-top, 14px);
    .scale(padding-left, 14px);
    .scale(padding-right, 14px);
    .scale(padding-bottom, 14px);

    & > .item {
      .scale(padding, 10px);
      .scale(margin-top, 6px);
      .scale(margin-bottom, 6px);
      text-align: center;
      flex-shrink: 0;
      .scale(width, 155px);
      // .scale(height, 135px);
    }

    & > a {
      &:hover {
        border-radius: 2px;
        box-shadow: 0px 0px 10px 0px rgba(83, 112, 239, 0.22);
      }
    }
    .label {
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }
  }
  /deep/.ant-carousel {
    .slick-slider {
      .scale(padding-left, 70px);
      .scale(padding-right, 70px);
    }
  }
}
</style>
